package yxy.game.pm2.command.arena.toparena;

import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.PlayerBean;
import yxy.game.pm2.bean.ServerBean;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.ArenaMessage;
import yxy.game.pm2.module.arena.toparena.TopAreanHandle;
import yxy.game.pm2.module.arena.toparena.TopAreanModule;
import yxy.game.pm2.module.arena.toparena.TopAreanPlayerModule;
import yxy.game.pm2.module.player.PlayerHandle;

import java.util.ArrayList;
import java.util.List;

/**
 * 巅峰赛界面
 *
 * @ClassName CMD_TopArenaUI
 * @Author jingtao-zheng
 * @Date 2022/9/7 17:11
 * @Version 1.0
 **/
@MessageType(proto = Command.TopArenaUI, classtype = ArenaMessage.TopArenaUIRequest.class)
public class CMD_TopArenaUI extends Command<ArenaMessage.TopArenaUIRequest> {

    @Override
    public void execute(MessageSource messageSource, ArenaMessage.TopArenaUIRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        ArenaMessage.TopArenaUIResponse.Builder response = ArenaMessage.TopArenaUIResponse.newBuilder();
        try {
            PlayerBean playerBean = PlayerBean.get(playerCode);
            int serverId = playerBean.getServerId();
            ServerBean serverBean = ServerBean.get(serverId);
            int areaId = serverBean.getAreaId();
            TopAreanModule topAreanModule = TopAreanModule.get(areaId);

            TopAreanHandle.TopAreanEnum state = topAreanModule.getState();//活动所处状态
            response.setState(ArenaMessage.TopArenaState.forNumber(state.getState().getNumber()));

            long millis = topAreanModule.getStateTimes().get(state).getMillis();
            response.setNextTime(millis);//状态结束时间

            //如果是活动未开启状态,不传玩家数据
            if (!state.equals(TopAreanHandle.TopAreanEnum.STEP_PREPARE)) {
                getplayerInfo(topAreanModule, response);
            }
            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        }
    }

    private void getplayerInfo(TopAreanModule topAreanModule, ArenaMessage.TopArenaUIResponse.Builder response) {
        List<String> allPlyaerCodes = topAreanModule.getPlyaerCodes();//所有参赛玩家
        List<ArenaMessage.TopArenaFighter.Builder> builders1_8 = new ArrayList<>();//非冠军组
        List<ArenaMessage.TopArenaFighter.Builder> builders9 = new ArrayList<>();//冠军组
        for (String playerCode : allPlyaerCodes) {
            ArenaMessage.TopArenaFighter.Builder builder = ArenaMessage.TopArenaFighter.newBuilder();
            builder.setPlayerInfo(new PlayerHandle().playerSimpleInfo(playerCode));//玩家信息
            TopAreanPlayerModule topAreanPlayerModule = TopAreanPlayerModule.get(playerCode);
            TopAreanHandle.TopAreanLevel nowLevel = topAreanPlayerModule.getNowLevel();
            int number = nowLevel.getNumber();
            /**
             * 转换给前端用
             */
            if (number == 9) {
                number = 4;
            }
            if (number == 8) {
                number = 3;
            }
            if (number == 7) {
                number = 2;
            }
            if (number == 6) {
                number = 1;
            }
            builder.setLevel(ArenaMessage.TopArenaRankLevel.forNumber(number));//名次
            int pos = topAreanPlayerModule.getPos();
            builder.setPos(pos);//位置
            int group = topAreanPlayerModule.getGroup();
            if (group == 9) {
                group = 0;//前端0,后端9代表冠军组,这里做一下转换
                builder.setGroup(group);
                builders9.add(builder);
            }
            builder.setGroup(group);//所属小组
            builders1_8.add(builder);
        }
        if (builders9.size() == 8) {
            for (ArenaMessage.TopArenaFighter.Builder builder : builders9) {
                response.addPlayers(builder);
            }
        } else {
            for (ArenaMessage.TopArenaFighter.Builder builder : builders1_8) {
                response.addPlayers(builder);
            }
        }
    }

    private void send(MessageSource messageSource, ArenaMessage.TopArenaUIResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
